package com.ying.service.system.impl;

import com.ying.core.exception.CustomException;
import com.ying.core.page.PageData;
import com.ying.core.page.PageWrap;
import com.ying.core.response.ResultCode;
import com.ying.dao.system.PermissionRepository;
import com.ying.po.system.Permission;
import com.ying.po.system.User;
import com.ying.service.system.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Service
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    private PermissionRepository permissionRepository;

    @Override
    public List<String> findPermissionsByUserType(Integer id) {
        List<Integer> permissionsIds = permissionRepository.findPermissionsByUserType(id);
        List<Permission> permissionList = permissionRepository.findAllById(permissionsIds);
        List<String> arrayList = new ArrayList<>();
        for (Permission permission : permissionList) {
            arrayList.add(permission.getName());
        }
        return arrayList;
    }

    @Override
    public Integer create(Permission permission) {
        if (permission.getId() != null) {
            Optional<Permission> optional = permissionRepository.findById(permission.getId());
            if (optional.isPresent()) {
                throw new CustomException(ResultCode.DATA_ALREADY_EXISTED, "PermissionServiceImpl.create");
            }
        }
        return permissionRepository.save(permission).getId();
    }

    @Override
    public Permission findById(Integer id) {
        // ------------------参数检验---------------
        if (id == null)
            throw new CustomException(ResultCode.PARAM_IS_BLANK, "PermissionServiceImpl.findById");
        // ------------开始业务----------------------
        Optional<Permission> optional = permissionRepository.findById(id);
        if (!optional.isPresent()) {
            throw new CustomException(ResultCode.RESULE_DATA_NONE, "PermissionServiceImpl.findById");
        }
        return optional.get();
    }

    @Override
    public PageData<Permission> findAllByPage(PageWrap<Permission> pageWrap) {
        // ------------------参数检验---------------
        if (!pageWrap.check()) {
            throw new CustomException(ResultCode.PARAM_IS_BLANK, "PermissionServiceImpl.findPage");
        }
        // ------------开始业务----------------------
        Pageable pageable = PageRequest.of(pageWrap.getPage(), pageWrap.getCapacity(), pageWrap.sortMethod());
        Page<Permission> permissionPage = permissionRepository.getPermissionPage(pageable);
        PageData<Permission> pageData = PageData.from(permissionPage);
        return pageData;
    }

    @Override
    public void updateById(Permission permission) {
// -------------------参数检验---------------------
        if (permission.getId() == null) {
            throw new CustomException(ResultCode.PARAM_IS_BLANK, "PermissionServiceImpl.updateById");
        }
        Optional<Permission> optional = permissionRepository.findById(permission.getId());
        if (!optional.isPresent()) {
            throw new CustomException(ResultCode.RESULE_DATA_NONE, "PermissionServiceImpl.updateById");
        }
        // ------------开始业务----------------------
        permissionRepository.save(permission);
    }

    @Override
    public void deleteById(Integer id) {
        // 参数检验----------------------------------
        if (id == null)
            throw new CustomException(ResultCode.PARAM_IS_BLANK, "PermissionServiceImpl.deleteById");
//        Optional<Permission> optional = permissionRepository.findById(id);
//        if (!optional.isPresent()) {
//            throw new CustomException(ResultCode.RESULE_DATA_NONE, "PermissionServiceImpl.deleteById");
//        }
        // ------------开始业务----------------------
        permissionRepository.deleteById(id);
    }

    @Override
    public List<Permission> findAll() {
        return permissionRepository.findAll();
    }

    @Override
    public void deleteByIds(List<Integer> ids) {
        for (Integer id : ids) {
            permissionRepository.deleteById(id);
        }

    }

    @Override
    public Integer createByName(String perName) {
        Permission permission = new Permission();
        permission.setName(perName);
        return this.create(permission);
    }

    @Override
    public List<Integer> findPermissionIdByRoleCode(Integer roleCode) {
        return permissionRepository.findPermissionIdByRoleCode(roleCode);
    }

    @Override
    public List<Integer> changePermissionByIdList(List<Integer> ids, Integer roleCode) {
        List<Integer> idsOld = this.findPermissionIdByRoleCode(roleCode);
        List<Integer> resultId = new ArrayList<>();

        for (Integer id : ids) {
            if (!idsOld.contains(id)) {
                // 存储这个新的id
                permissionRepository.createNewPerIdToRole(id, roleCode);
                resultId.add(id);
            }
        }
        for (Integer id : idsOld) {
            if (!ids.contains(id)) {
//                删除这个旧id
                permissionRepository.deleteOldPerIdToRole(id, roleCode);
                resultId.add(id);
            }
        }
        return resultId;
    }
}
